package cn.fanqinghong.user.service.impl;

import cn.fanqinghong.basic.constants.BaseConstants;
import cn.fanqinghong.basic.dto.RegisterDto;
import cn.fanqinghong.basic.exception.BusinessException;
import cn.fanqinghong.basic.util.MD5Utils;
import cn.fanqinghong.basic.util.StrUtils;
import cn.fanqinghong.user.domain.Logininfo;
import cn.fanqinghong.user.domain.User;
import cn.fanqinghong.user.mapper.LogininfoMapper;
import cn.fanqinghong.user.mapper.UserMapper;
import cn.fanqinghong.user.service.IUserService;
import cn.fanqinghong.basic.service.impl.BaseServiceImpl;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fanqinghong
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void phoneRegister(RegisterDto dto) {
        String phone = dto.getPhone();
        String smsCode = dto.getSmsCode();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();
        //1.校验
        // 空校验
        if(phone.isEmpty()){
            throw new BusinessException("电话号码不能为空");
        }
        if( smsCode.isEmpty() ){
            throw new BusinessException("验证码不能为空");
        }
        if(password.isEmpty() ){
            throw new BusinessException("密码不能为空");
        }
        if(passwordRepeat.isEmpty()){
            throw new BusinessException("验证密码不能为空");
        }
        // 密码和确认密码是否一致
        if (!password.equals(passwordRepeat)){
            throw new BusinessException("两次输入的密码不一致，请核验后重新输入!!");
        }
        //2.短信验证码判断
        //拼接电话号码
        String key = BaseConstants.RegisterConstants.REGISTER_SMS_PREFIX + phone;
        /*String smsCodeValue = smsCode + ":" + System.currentTimeMillis();这里是设置的key的value值*/
        //获取redis中的电话号码对应的value
        Object smsCodeTmp = redisTemplate.opsForValue().get(key);
        //2.1 拿不到  验证码过期，请重新获取
        if (smsCodeTmp == null){
            throw new BusinessException("验证码已过期，请重新获取!!");
        }
        //2.2 拿到了，判断是否一致
        if (!smsCode.equalsIgnoreCase(smsCodeTmp.toString().split(":")[0])){
            //  。 不一致 验证码错误，请核对
            throw new BusinessException("验证码错误，请重新输入!!");
        }
        //  。一致，往下走
        //3.保存数据 t_user + t_logininfo
        User user = dto2User(dto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    @Override
    public User loadByLogininfoId(Long id) {
        return userMapper.loadByLogininfoId(id);
    }

    /**
     * 重新设置loginfo对象
     * @param user
     * @return
     */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //使用spring提供的工具类，把user中loginfo相同字段复制给它
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//这里设置的是用户注册
        return logininfo;
    }

    /**
     * 重新设置user对象
     * @param dto
     * @return
     */
    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        /**
         * 盐值
         */
        String salt = StrUtils.getComplexRandomString(32);
        /**
         * 密码，md5加密加盐
         */
        String md5Pwd  = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        //设置盐值
        user.setSalt(salt);
        //设置密码
        user.setPassword(md5Pwd);
        return user;
    }

    @Override
    public void removeById(Long id) {
        User user = userMapper.loadById(id);
        logininfoMapper.deleteById(user.getLogininfoId());
        userMapper.deleteById(id);
    }
}
